#include "func.h"

int main()
{
    //1.socket
    int clientfd = socket(AF_INET,SOCK_STREAM,0);
    if(clientfd < 0){
        perror("socket");
        return EXIT_FAILURE;
    }

    //2.connect
    struct sockaddr_in serveraddr;
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(8000);
    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    int ret = connect(clientfd,(const struct sockaddr*)&serveraddr,sizeof(serveraddr));
    if(ret < 0){
        perror("connect");
        close(clientfd);
        return EXIT_FAILURE;
    }
    printf("connect success\n");

    while(1){
        char buf[1024] = {0};
        ret = recv(clientfd,buf,sizeof(buf),0);
        printf("recv %d bytes, buf : %s \n",ret,buf);
        
        memset(buf,0,sizeof(buf));
        int ret = read(STDIN_FILENO,buf,sizeof(buf));
        ret = send(clientfd, buf,strlen(buf),0); 
        printf("send %d bytes \n",ret);
    }


    return 0;
}

